package com.uyayo.system.monitor.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.uyayo.framework.common.exception.CustomException;
import com.uyayo.framework.pagehelper.pojo.OrderBy;
import com.uyayo.framework.pagehelper.pojo.PageBean;
import com.uyayo.framework.pagehelper.util.OrderByUtil;
import com.uyayo.system.monitor.entity.SysEvent;
import com.uyayo.system.monitor.mapper.SysEventMapper;
import com.uyayo.system.monitor.model.SysEventCondition;
import com.uyayo.system.monitor.service.EventsService;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;


@Service("eventsService")
public class EventsServiceImpl  implements EventsService {

	private final SysEventMapper sysEventMapper;

	@Autowired
	public EventsServiceImpl(SysEventMapper sysEventMapper) {
		this.sysEventMapper = sysEventMapper;
	}

	@Override
	public SysEvent getEventById(String id) throws Exception{
		SysEvent events =sysEventMapper.selectByPrimaryKey(id);
		if(events==null){
			throw new CustomException("该系统监听事件记录不存在！");
		}
		return events;
	}

	@Override
	public  PageBean<SysEvent> selectEvent(Integer pageCurrent, Integer pageSize,String sort,String order,SysEventCondition sysEventCondition){
		List<SysEvent> events;
		//分页处理
		List<OrderBy> orderBies = new ArrayList<>();
		orderBies.add(new OrderBy("startTime", "start_time"));
		orderBies.add(new OrderBy("endTime", "end_time"));
		orderBies.add(new OrderBy("created", "created"));

		OrderByUtil orderByUtil = new OrderByUtil();
		String orderBy = orderByUtil.getOrderBy(orderBies, sort, order);
		PageHelper.startPage(pageCurrent, pageSize);
		
		Example example = new Example(SysEvent.class);
		if(!StringUtils.isEmpty(orderBy)){
			example.setOrderByClause(orderBy);
		}
		Criteria criteria = example.createCriteria();
		if(!StringUtils.isEmpty(sysEventCondition.getMethodName())){
			criteria.andLike("methodName","%"+sysEventCondition.getMethodName()+"%");
		}
		if(!StringUtils.isEmpty(sysEventCondition.getDescription())){
			criteria.andLike("description","%"+sysEventCondition.getDescription()+"%");
		}
		if(sysEventCondition.getBeginCreatedTime()!=null || sysEventCondition.getEndCreatedTime()!=null){
			criteria.andBetween("startTime", sysEventCondition.getBeginCreatedTime(), sysEventCondition.getEndCreatedTime());
		}
		events = sysEventMapper.selectByExample(example);
		//取记录总条数
		PageInfo<SysEvent> pageInfo = new PageInfo<>(events);
			
		return new PageBean<>(pageInfo.getTotal(), pageCurrent, pageSize, events);

	}


	@Override
	public  List<SysEvent> selectEvent(SysEventCondition sysEventCondition){
		List<SysEvent> events;

		Example example = new Example(SysEvent.class);

		Criteria criteria = example.createCriteria();
		if(!StringUtils.isEmpty(sysEventCondition.getMethodName())){
			criteria.andLike("methodName","%"+sysEventCondition.getMethodName()+"%");
		}
		if(!StringUtils.isEmpty(sysEventCondition.getDescription())){
			criteria.andLike("description","%"+sysEventCondition.getDescription()+"%");
		}
		if(sysEventCondition.getBeginCreatedTime()!=null || sysEventCondition.getEndCreatedTime()!=null){
			criteria.andBetween("startTime", sysEventCondition.getBeginCreatedTime(), sysEventCondition.getEndCreatedTime());
		}
		events = sysEventMapper.selectByExample(example);
		return events;
	}

	@Override
	public int deleteEvent(String ids) {
		if (!StringUtils.isEmpty(ids)) {
			String[] idArr = ids.split(",");
			for (String anIdArr : idArr) {
				if (!anIdArr.equals("")) {
					sysEventMapper.deleteByPrimaryKey(anIdArr);
				}
			}
			return 1;
		}else{
			return 0;
		}
	}

	@Override
	public int deleteEvent(String ids,SysEventCondition sysEventCondition) {

		if (!StringUtils.isEmpty(ids)) {
			String[] idArr = ids.split(",");
			for (String anIdArr : idArr) {
				if (!anIdArr.equals("")) {
					sysEventMapper.deleteByPrimaryKey(anIdArr);
				}
			}
			return 1;
		}else{

			Example example = new Example(SysEvent.class);

			Criteria criteria = example.createCriteria();
			if(!StringUtils.isEmpty(sysEventCondition.getMethodName())){
				criteria.andLike("methodName","%"+sysEventCondition.getMethodName()+"%");
			}
			if(!StringUtils.isEmpty(sysEventCondition.getDescription())){
				criteria.andLike("description","%"+sysEventCondition.getDescription()+"%");
			}
			if(sysEventCondition.getBeginCreatedTime()!=null || sysEventCondition.getEndCreatedTime()!=null){
				criteria.andBetween("startTime", sysEventCondition.getBeginCreatedTime(), sysEventCondition.getEndCreatedTime());
			}
			return sysEventMapper.deleteByExample(example);
		}
	}
	
	@Override
	public int insertSelective(SysEvent events) {
		return sysEventMapper.insertSelective(events);
	}
}
